package org.mnzd.tree.btree;

import java.util.List;




/**
 * 树模型
 * @author 李声波
 *
 */
public class TreeModel {

	private String id;
	//节点路径
	private String path;
	private String text;
	private String icon;
	private Object children=false;
	private NodeType type=NodeType.file;
	private TreeState state;
	private LiAttr li_attr;
	private Aattr a_attr;
	//父节点不序列化
	private TreeModel parent;

	
	
	
	public TreeModel() {
		super();
		// TODO Auto-generated constructor stub
	}

	
	
	/**
	 * 树节点构造方法
	 * @param id
	 * @param text
	 */
	public TreeModel(String id, String text) {
		super();
		this.id = id;
		this.text = text;
	}




	/**
	 * 构节点构造方法
	 * @param id 节点ID
	 * @param text 节点显示文本
	 * @param children 是否包含子节点
	 * @param isOpened 是否展开节点
	 * @param isSelected 是否选中节点，由开发人员编写SQL实现
	 */
	public TreeModel(String id,String text,Long children,boolean isOpened,String isSelected) {
		super();
		this.id = id;
		this.text = text;
		//子节点总数大于0，则为根节点
		if(children>0)
		{
			this.children = true;
			type=NodeType.root;
		}
		TreeState ts=new TreeState();
		ts.setDisabled(false);
		ts.setOpened(isOpened);
		if(isSelected!=null && "".equals(isSelected))
		{
			ts.setSelected(true);			
		}
		this.setState(ts);
	}

	public TreeModel(String id,String text,Long children,boolean isOpened,String isSelected,String path) {
		super();
		this.id = id;
		this.text = text;
		//子节点总数大于0，则为根节点
		if(children>0)
		{
			this.children = true;
			type=NodeType.root;
		}
		TreeState ts=new TreeState();
		ts.setDisabled(false);
		ts.setOpened(isOpened);
		if(isSelected!=null && "".equals(isSelected))
		{
			ts.setSelected(true);			
		}
		this.setState(ts);
		this.setPath(path);
	}
	/**
	 * 创建一个新节点
	 * @param id
	 * @param text
	 * @param children
	 * @param isOpened
	 * @param isSelected
	 */
	public TreeModel(String id,String text,TreeModel parent,Object children,boolean isOpened,String isSelected) {
		super();
		this.id = id;
		this.text = text;
		this.children=children;
		this.parent=parent;
		TreeState ts=new TreeState();
		ts.setDisabled(false);
		ts.setOpened(isOpened);
		if(isSelected!=null && "".equals(isSelected))
		{
			ts.setSelected(true);			
		}
		this.setState(ts);
	}
	
	/**
	 * 树节点构造方法
	 * @param id
	 * @param text
	 * @param parent
	 * @param children
	 * @param isOpened
	 * @param isSelected
	 * @param path
	 */
	public TreeModel(String id,String text,TreeModel parent,Object children,boolean isOpened,String isSelected,String path) {
		super();
		this.id = id;
		this.text = text;
		this.children=children;
		this.parent=parent;
		TreeState ts=new TreeState();
		ts.setDisabled(false);
		ts.setOpened(isOpened);
		if(isSelected!=null && "".equals(isSelected))
		{
			ts.setSelected(true);			
		}
		this.setState(ts);
		this.setPath(path);
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public TreeState getState() {
		return state;
	}

	public void setState(TreeState state) {
		this.state = state;
	}

	public LiAttr getLi_attr() {
		return li_attr;
	}

	public void setLi_attr(LiAttr li_attr) {
		this.li_attr = li_attr;
	}

	public Aattr getA_attr() {
		return a_attr;
	}

	public void setA_attr(Aattr a_attr) {
		this.a_attr = a_attr;
	}

	

	public Object getChildren() {
		return children;
	}

	@SuppressWarnings("unchecked")
	public void setChildren(Object children) {
		if(children instanceof List)
		{
			((List<TreeModel>) children).forEach(node->{
				if(node!=null)
				{
					node.setParent(this);					
				}
			});
		}
		this.children = children;
	}

	public NodeType getType() {
		return type;
	}

	public void setType(NodeType type) {
		this.type = type;
	}



	public TreeModel getParent() {
		return parent;
	}



	public void setParent(TreeModel parent) {
		this.parent = parent;
	}

	public String getPath() {
		return path;
	}



	/**
	 * 设置树的路径
	 * @param path
	 */
	public void setPath(String path) {
		this.path = path;
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((text == null) ? 0 : text.hashCode());
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TreeModel other = (TreeModel) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (text == null) {
			if (other.text != null)
				return false;
		} else if (!text.equals(other.text))
			return false;
		return true;
	}

}

class State {
	private boolean opened;
	private boolean disabled;
	private boolean selected;
	public boolean isOpened() {
		return opened;
	}
	public void setOpened(boolean opened) {
		this.opened = opened;
	}
	public boolean isDisabled() {
		return disabled;
	}
	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}
	public boolean isSelected() {
		return selected;
	}
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	
	
	
}

class LiAttr {
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}

class Aattr {
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}